import common.CommonDemo;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.time.Duration;

public class Alerts {

    CommonDemo commonDemo = new CommonDemo();
    private ChromeDriver driver;
    private String webUrl;
    private String url = "src/main/resources/Alerts.html";
    private WebDriverWait wait;

    @BeforeTest
    public void setUp() throws Exception{
        //获取chromedriver
        driver = commonDemo.getChromeDriver();
        webUrl = commonDemo.getPropertyValue("config.properties", "web.url.baidu");
        wait = new WebDriverWait(driver, Duration.ofSeconds(2));
    }

    //file:///D:/springDemo/SeleniumLearn/src/main/resources/Alerts.html
    @Test
    public void test() throws Exception{
        String filepath = commonDemo.getUserDir(url);
        driver.get(filepath);
        //等待元素显示并点击
        WebElement element = driver.findElement(By.xpath("//input[@value='点击']"));
        wait.until(ExpectedConditions.elementToBeClickable(element)).click();
        //等待Alert弹窗显示并点击确定
        Alert alert = wait.until(ExpectedConditions.alertIsPresent());
        String text = alert.getText();
        System.out.println(text);
        Thread.sleep(1000);
        alert.accept();
    }

    @AfterClass
    public void tearDown() throws Exception{
        Thread.sleep(2000);
        driver.quit();
    }
}
